package com.kongchengji.spider.station.pipeline;

import com.kongchengji.spider.station.constant.Constant;
import com.kongchengji.spider.station.domain.PictureModelDO;
import com.kongchengji.spider.util.*;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import java.util.ArrayList;
import java.util.Map;

/**
 * 爬取结果处理
 */
public class StationPipeline implements Pipeline {
    private Constant.Station station;
    //是否需要从栏目中提取标题标记
    private boolean isManageTitle = true;
    public StationPipeline(Constant.Station station){
        this.station = station;
    }
    @Override
    public void process(ResultItems resultItems, Task task) {
        Map<String, Object> map = resultItems.getAll();
        if(map.size() != 0){
            PictureModelDO pictureModelDO = new PictureModelDO();
            //设置采集网站的质量
            pictureModelDO.setQuality(station.getQuality());
            //设置采集源
            pictureModelDO.setStationId(station.getId());
            ArrayList<String> typeList = (ArrayList<String>)map.get("typeList");
            ArrayList<String> urlContentList = ManageImgSrcUtil.manageImgSrc(station,(ArrayList<String>)map.get("urlContentList"));
            String title;
            if(map.get("title") != null){
                title = (String)map.get("title");
                pictureModelDO.setTitle(title);
                isManageTitle = false;
            }
            //
            if(isManageTitle){
                ManageTypeListUtil.manageType2(typeList,pictureModelDO);
            }else {
                ManageTypeListUtil.manageType1(typeList,(String)map.get("title"),pictureModelDO);
            }
            //排除为null的元素
            RemoveNullUtil.removeNull(urlContentList);
            pictureModelDO.setUrlContentList(urlContentList);
            //当时测试时将爬取的数据存储在本地数据库。
//            R r = pictureModelService.add(pictureModelDO,station.getId());
//            logger.info(r.toString());
            //通过接口更新采集后台数据.
            AccessInterfaceUtil.accessInterface(pictureModelDO);
        }
    }
}
